// pages/index/component/advertisement/advertisement.js
import { list } from '../../../../api/ad'
const util = require('../../../../utils/util')
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object,
    }
  },
  lifetimes: {
    attached() {
      var arr = Object.keys(this.data.info.tagids)
      if (arr.length > 0) {
        this.data.tagIds = this.data.info.tagids.split(',')
        console.log(this.data.tagIds)
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
        this.setData({
          tagIds: this.data.tagIds
        })
      }
      this.loadAd()
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    adList: [],
    tagIds: [],

  },

  /**
   * 组件的方法列表
   */
  methods: {
    toLink(e) {
      console.info("广告组件被点击e--" + JSON.stringify(e))
      if (e.currentTarget.dataset.type.hasOwnProperty('typeId') && !util.isNull(e.currentTarget.dataset.type.typeId)) {
        if (e.currentTarget.dataset.type.type === 0) {
          wx.navigateTo({
            url: '/pages/goods/product/details?id=' + e.currentTarget.dataset.type.typeId
          })
        } else if (e.currentTarget.dataset.type.type === 9) {
          wx.navigateTo({
            url: '/pages/index/wxPage/wxPage?id=' + e.currentTarget.dataset.type.typeId
          })
        } else if (e.currentTarget.dataset.type.type === 1) {
          wx.navigateTo({
            url: '/pages/goods/category/category?id=' + e.currentTarget.dataset.type.typeId
          })
        } else if (e.currentTarget.dataset.type.type === 2) {
          wx.navigateTo({
            url: '/pages/member/detail/detail?id=' + e.currentTarget.dataset.type.typeId,
          })
        } else if (e.currentTarget.dataset.type.type === 3) {
          wx.navigateTo({
            url: '/pages/member/vip/vip'
          })
        } else if (e.currentTarget.dataset.type.type === 12) {
          /** 
           * 打开另一个小程序
           * appId，path
           **/
          console.log(e.currentTarget.dataset.type)
          const { typeName, url } = e.currentTarget.dataset.type
          if (typeName.indexOf("mp://") == 0 || url.indexOf("mp://") == 0) {
            if ((typeName.indexOf("appId") > 0 || url.indexOf("appId") > 0) && (typeName.indexOf("path") > 0 || url.indexOf("path") > 0)) {
              let appId_index = typeName.indexOf('appId=',)
              let path_index = typeName.indexOf('&path=',)
              let appId = typeName.substring(appId_index + 6, path_index)
              let path = typeName.substring(path_index + 6)
              // console.log(appId, path, typeName.length)
              wx.navigateToMiniProgram({
                appId: appId,
                path: path,
                success: res => {
                  console.log(res)
                },
                fail: err => {
                  console.log(err)
                },
              })
            }
          }
        }
      }
    },
    loadAd() {
      list({
        tagIds: this.data.tagIds,
        mchId: app.globalData.mchId,
      }).then(res => {
        // console.info("广告组件的点击事件")
        // console.info(JSON.stringify(res.data.data))
        this.data.adList = res.data.data
        this.setData({
          adList: this.data.adList
        })
      })
    }
  }
})
